package com.company.app.boImpl;

import java.util.List;

import com.company.app.bo.CustomerBO;
import com.company.app.dao.CustomerDAO;
import com.company.app.entities.Customer;

/**
 * The Class CustomerBOImp.
 */
/**
 * @author projas
 * 
 */
public class CustomerBOImpl implements CustomerBO {

	private CustomerDAO customerDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.bo.CustomerBO#getCustomerById(java.lang.Long)
	 */
	@Override
	public Customer getCustomerById(Long id) {
		return this.customerDAO.getEntityById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.CustomerBO#searchCustomerByFilter(com.company.app.
	 * entities.Customer)
	 */
	@Override
	public List<Customer> searchCustomers(List<Customer> customer) {
		return this.customerDAO.searchEntities(customer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.bo.CustomerBO#executeCustomerBussinessLogic()
	 */
	@Override		
	public boolean executeCustomerBussinessLogic() {
		Customer customer = this.customerDAO.getEntityById(1L);
		customer.setAddress("New Street 10a, 5000 City");
		this.customerDAO.updateEntity(customer);
		return false;				
	}
	
	/**
	 * Sets the customer dao.
	 * 
	 * @param customerDAO
	 *            the new customer dao
	 */
	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.CustomerBO#createCustomer(com.company.app.entities
	 * .Customer)
	 */
	@Override		
	public Customer createCustomer(Customer customer) {
		return this.customerDAO.createEntity(customer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.CustomerBO#updateCustomer(com.company.app.entities
	 * .Customer)
	 */
	@Override
	public boolean updateCustomer(Customer entity) {
		return this.customerDAO.updateEntity(this.buildEntity(entity))!=null;
	}
	
	/*
	 * GET THE CUSTOMER FROM THE DB AND THEN UPDATE
	 * THIS IS DON'T TO UPDATE THE OBJECT WITH ALL THE INFO
	 * IN THE CACHE
	 */
	private Customer buildEntity(Customer param){
		Customer customerEntity= this.getCustomerById(param.getId());
		if (customerEntity!=null){
			if (param.getAddress()!=null){
				customerEntity.setAddress(param.getAddress());
			}
			if(param.getEmail()!=null){
				customerEntity.setEmail(param.getEmail());
			}
			if (param.getName()!=null){
				customerEntity.setName(param.getName());
			}
		}
		return customerEntity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.CustomerBO#deleteCustomer(com.company.app.entities
	 * .Customer)
	 */
	@Override		
	public boolean deleteCustomer(Customer customer) {
		this.customerDAO.deleteEntity(customer);
		return true;
	}

}
